﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Configuration;
using System.Collections;
using System.Reflection;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;

namespace DAO.WaiGaoQiao
{
    public static class GetSqlStatements
    {
        /// <summary>
        /// 获取Sql语句
        /// </summary>
        /// <param name="xmlfile">有sql语句的xml文件路径</param>
        /// <param name="selname">sql语句节点名称</param>
        /// <param name="ht"></param>
        /// <returns></returns>
        public static string GetSelect(string xmlfile,string selname, IDictionary ht)
        {
            XmlDocument xmldocu = new XmlDocument();

            string selxmlpath = HttpContext.Current.Server.MapPath(xmlfile);//"/ConfigXml/DataSql.xml"
            XmlDocument doc = new XmlDocument();
            doc.Load(selxmlpath);

            var getElement = doc.DocumentElement.GetElementsByTagName(selname);
            string sqlstatement = getElement[0].InnerText.Trim();
            if (getElement[0].Attributes["parameterClass"].Value == "System.Collections.IDictionary")
            {
                foreach (DictionaryEntry de in ht)
                {
                    int keyindex = sqlstatement.IndexOf(de.Key.ToString());
                    string repsubstr = sqlstatement.Substring(keyindex-1, de.Key.ToString().Length+2);
                    sqlstatement = sqlstatement.Replace(repsubstr, de.Value.ToString());
                }
            }
            return sqlstatement;
        }

        /// <summary>
        /// 当查询条件的参数是字符时使用这个方法
        /// </summary>
        /// <param name="selname"></param>
        /// <param name="query">parameterClass="System.String"字符类型</param>
        /// <returns></returns>
        public static string GetSelect(string xmlfile, string selname, string query)
        {
            XmlDocument xmldocu = new XmlDocument();

            string selxmlpath = HttpContext.Current.Server.MapPath(xmlfile);//"/ConfigXml/DataSql.xml"
            XmlDocument doc = new XmlDocument();
            doc.Load(selxmlpath);

            var getElement = doc.DocumentElement.GetElementsByTagName(selname);
            string sqlstatement = getElement[0].InnerText.Trim();
            if (getElement[0].Attributes["parameterClass"].Value == "System.String")
            {
                int firstchar = sqlstatement.IndexOf('#');
                int senchar = sqlstatement.IndexOf('#', firstchar + 1);
                sqlstatement = sqlstatement.Replace(sqlstatement.Substring(firstchar, senchar - firstchar + 1), query);
            }
            return sqlstatement;
        }

        public static string ExeInsert(string xmlfile, string insertname, IDictionary ht)
        {
            XmlDocument xmldocu = new XmlDocument();

            string selxmlpath = HttpContext.Current.Server.MapPath(xmlfile);//"/ConfigXml/DataSql.xml"
            XmlDocument doc = new XmlDocument();
            doc.Load(selxmlpath);
            var getElement = doc.DocumentElement.GetElementsByTagName(insertname);
            string sqlstatement = getElement[0].InnerText.Trim();
            //AssemblyName assname = new AssemblyName("Model.One");
            //Assembly ably = Assembly.Load(assname);
            //Type[] tt = ably.GetTypes();
            foreach (DictionaryEntry de in ht)
            {
                Type t = de.Value.GetType();
                PropertyInfo[] pinfo = t.GetProperties();// ("UserName");
                object obj;
                foreach (PropertyInfo singpinfo in pinfo)
                {
                    string filedname = singpinfo.Name;
                    obj = singpinfo.GetValue(de.Value, null);

                    int keyindex = sqlstatement.IndexOf("#"+filedname+"#");
                    string repsubstr = sqlstatement.Substring(keyindex, filedname.Length + 2);
                    switch (obj.GetType().ToString())
                    {
                        case "System.String":
                        case "System.DateTime":
                        case "System.Char":
                            sqlstatement = sqlstatement.Replace(repsubstr, "'"+obj.ToString()+"'");
                            break;
                        case "System.Int32":
                        case "System.Int64":
                        case "System.Decimal":
                        case "System.Single":
                        case "System.Int16":
                        case "System.Double":
                            sqlstatement = sqlstatement.Replace(repsubstr, obj.ToString());
                            break;
                        default:
                            break;
                    }

                }                
            }
            return sqlstatement;
        }
    }

    public static class GetConntionString
    {
        public static string conntionstring()
        {
            return ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString.Trim();
        }
    }
}
